module ActiveScaffold::Config
  class Core < Base

    # configures where the dry_ActiveScaffold plugin itself is located. there is no instance version of this.
    cattr_accessor :dry_plugin_directory
    @@dry_plugin_directory = __FILE__.match(/vendor\/plugins\/(\w*)/)[1] rescue ''
    
    def exclude_columns(action, columns)
      exclude_set = columns.collect {|col| col[:name] if !col[:exclude].nil? and col[:exclude].include?(action)}.compact      
      eval("#{action.to_s}.columns.exclude exclude_set") unless exclude_set.nil?
    end

    # TODO columns[:phone].set_link('nested', :parameters => {:associations => :contacts})
    def has_columns(column_options)
      self.columns = column_options.collect {|col| col[:name].to_sym}
      exclude_columns :create, column_options
      exclude_columns :list, column_options
      # exclude_columns :search, columns
      exclude_columns :show, column_options
      exclude_columns :update, column_options
      column_options.each do |col|
#        col.reject {|key, value| [:name, :type].include?(key.to_sym)}.each do |key, value|
        col.each do |key, value|
          case key
          when :sorting
            eval "list.#{key} = {:#{col[:name]} => value}"
          else
            next if [:name, :type].include?(key)
            col_method = "columns[:#{col[:name]}].#{key}"
            if ActiveScaffold::DataStructures::Column.method_defined?("#{key}=")
              eval "#{col_method} = value"
            elsif ActiveScaffold::DataStructures::Column.method_defined?("#{key}") 
              eval "#{col_method} value"
            end
          end
        end
      end
    end
    
    def self.template_search_path
      search_path = [ "active_scaffold_overrides" ]
      search_path << "../../vendor/plugins/#{ActiveScaffold::Config::Core.dry_plugin_directory}/frontends/default/views"
      search_path << "../../vendor/plugins/#{ActiveScaffold::Config::Core.plugin_directory}/frontends/#{ActiveScaffold::Config::Core.frontend.to_s}/views" if ActiveScaffold::Config::Core.frontend != :default
      search_path << "../../vendor/plugins/#{ActiveScaffold::Config::Core.plugin_directory}/frontends/default/views"
# ActionController::Base::logger.debug "xxxxxx#{search_path}xxxxxxx"
      search_path
    end

  end
end